﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using BusinessLayer.Tables;

public partial class DataList : MasterPage
{
    public enum PageType
    {
        Cari,
        Stok,
        Reyon,
        Depo,
        Fiyat,
        Pos,
        Users,
        Firma,
        Donem,
        Promosyon,
        HizliPromosyon
    }

    string adres = string.Empty;

    public PageType SayfaTipi
    {
        get
        {
            PageType s = (PageType)ViewState["pagetype"];
            return s;
        }
        set
        {
            ViewState["pagetype"] = value;
        }
    }

    protected override void OnInitComplete(EventArgs e)
    {
        string param = string.Empty;
        ListItem lst;
        ListItem lst2;
        if (Request.QueryString.Count > 0)
        {
            param = Request.QueryString["type"].ToString();
        }
        if (!IsPostBack)
        {
            switch (param)
            {
                case "Musteri":
                    SayfaTipi = PageType.Cari;
                    addAttribute(newcustomer, "Customer");
                    lst = new ListItem();
                    lst.Text = "Müşteri Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Müşteri Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    adres = "Customer";
                    break;
                case "Depo":
                    SayfaTipi = PageType.Depo;
                    lst = new ListItem();
                    lst.Text = "Depo Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Depo Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    addAttribute(newcustomer, "Admin/Depo");
                    adres = "Admin/Depo";
                    break;
                case "Fiyat":
                    SayfaTipi = PageType.Fiyat;
                    lst = new ListItem();
                    lst.Text = "Fiyat Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Fiyat Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    addAttribute(newcustomer, "Admin/FiyatTanim");
                    adres = "Admin/FiyatTanim";
                    break;

                case "Pos":
                    SayfaTipi = PageType.Pos;
                    lst = new ListItem();
                    lst.Text = "Pos Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Pos Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    addAttribute(newcustomer, "Admin/Poslar");
                    adres = "Admin/Poslar";
                    break;
                case "Users":
                    SayfaTipi = PageType.Users;
                    addAttribute(newcustomer, "Admin/Users");
                    lst = new ListItem();
                    lst.Text = "Kullanıcı Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Kullanıcı Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    adres = "Admin/Users";
                    break;
                case "Reyon":
                    SayfaTipi = PageType.Reyon;
                    addAttribute(newcustomer, "");
                    break;
                case "Stok":
                    SayfaTipi = PageType.Stok;
                    addAttribute(newcustomer, "Stok");
                    lst = new ListItem();
                    lst.Text = "Stok Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Stok Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    adres = "Stok";
                    break;
                case "Firma":
                    SayfaTipi = PageType.Firma;
                    addAttribute(newcustomer, "Admin/FirmaTanitim");
                    lst = new ListItem();
                    lst.Text = "Firma Adına Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Firma Koduna Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    adres = "Admin/FirmaTanitim";
                    break;
                case "Donem":
                    SayfaTipi = PageType.Donem;
                    addAttribute(newcustomer, "Admin/Donem");
                    lst = new ListItem();
                    lst.Text = "Donem Koduna Göre";
                    lst.Value = "0";
                    lst2 = new ListItem();
                    lst2.Text = "Firma Numarasına Göre";
                    lst2.Value = "1";
                    drpFiltre.Items.Add(lst);
                    drpFiltre.Items.Add(lst2);
                    adres = "Admin/Donem";
                    break;
                case "Promosyon":
                    SayfaTipi = PageType.Promosyon;
                    addAttribute(newcustomer, "Promosyon");
                    lst = new ListItem();
                    lst.Text = "Promosyon Koduna Göre";
                    lst.Value = "0";
                    drpFiltre.Items.Add(lst);
                    lst = new ListItem();
                    lst.Text = "Promosyon Adına Göre";
                    lst.Value = "1";
                    drpFiltre.Items.Add(lst);
                    adres = "Promosyon";
                    break;
                case "HizliPromosyon":
                    SayfaTipi = PageType.HizliPromosyon;
                    addAttribute(newcustomer, "HizliPromosyon");
                    lst = new ListItem();
                    lst.Text = "Promosyon Koduna Göre";
                    lst.Value = "0";
                    drpFiltre.Items.Add(lst);
                    lst = new ListItem();
                    lst.Text = "Promosyon Adına Göre";
                    lst.Value = "1";
                    drpFiltre.Items.Add(lst);
                    adres = "Promosyon";
                    break;
            }
        }
        this.Page.ClientScript.RegisterStartupScript(typeof(Page), "Test", "AdresGonder('" + adres + "')", true);
        base.OnInitComplete(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack && !Grid1.CausedCallback)
        {
            buildGrid();
            Grid1.DataBind();
        }
    }

    private void addAttribute(System.Web.UI.WebControls.ImageButton btn, string adres)
    {
        if (adres == "Stok")
        {
            string strScript = "javascript:";
            strScript += "window.open('" + adres + ".aspx ";
            strScript += "','_blank',";
            strScript += "'width=900"; // Height
            strScript += ",height=650"; // Width
            strScript += ",scrollbars=1"; // Scrollbar
            strScript += "'); return false;";
            btn.Attributes.Add("onclick", strScript);
        }
        else
        {
            string strScript = "javascript:";
            strScript += "window.open('" + adres + ".aspx ";
            strScript += "','_blank',";
            strScript += "'width=800"; // Height
            strScript += ",height=650"; // Width
            strScript += ",scrollbars=1"; // Scrollbar
            strScript += "'); return false;";
            btn.Attributes.Add("onclick", strScript);
        }
    }

    private void buildGrid()
    {
        MasterTable tablo;
        List<string> query = new List<string>();
        switch (SayfaTipi)
        {
            case PageType.Cari:
                query.Add("musteri_kod as [Müşteri Kodu]");
                query.Add("musteri_adi AS [Müşteri Adı]");
                query.Add("kart_sube AS [Kart Şubesi]");
                query.Add("ev_ilce AS [Ev Adresi İlçe]");
                query.Add("is_ilce AS [İş Adresi İlçe]");
                tablo = new Db_Musteri();
                break;
            case PageType.Stok:
                query.Add("stok_kodu AS [Stok Kodu]");
                query.Add(" stok_adi AS [Stok Adı]");
                tablo = new Db_Stok();
                break;
            case PageType.Reyon:
                tablo = new MasterTable();
                break;
            case PageType.Depo:
                query.Add("depo_kodu AS [Depo Kodu]");
                query.Add(" depo_adi AS [Depo Adı]");
                tablo = new Db_Depo();
                break;
            case PageType.Fiyat:
                query.Add("fyt_kod AS [Fiyat Kodu]");
                query.Add(" fyt_adi AS [Fiyat Adı]");
                tablo = new Db_FiyatTanim();
                break;
            case PageType.Pos:
                query.Add("cash_id AS [Kasa No]");
                query.Add("cash_depo_id as [Depo No]");
                query.Add(" cash_adi AS [Kasa Adı]");
                tablo = new Db_Poslar();
                break;
            case PageType.Users:
                query.Add("usr_kod AS [Kullanici No]");
                query.Add("usr_adi AS [Kullanici Adi]");
                query.Add(" usr_tel AS [Kullanici Telefon]");
                query.Add(" usr_email AS E_mail");
                tablo = new Db_Users();
                break;
            case PageType.Firma:
                query.Add("frm_kod AS [Firma Kod]");
                query.Add(" frm_adi AS [Firma Adı]");
                query.Add("frm_adres AS [Firma Adres]");
                tablo = new Db_Firma();
                break;
            case PageType.Donem:
                query.Add("dnm_kod AS [Dönem Kod]");
                query.Add(" dnm_firma_id AS [Firma No]");
                tablo = new Db_Donem();
                break;
            case PageType.Promosyon:
                query.Add("proh_kod as [Promosyon Kodu]");
                query.Add(" proh_adi AS [Promosyon Adı]");
                tablo = new PromoHeader();
                break;
            case PageType.HizliPromosyon:
                query.Add("proh_kod as [Promosyon Kodu]");
                query.Add(" proh_adi AS [Promosyon Adı]");
                tablo = new PromoHeader();
                break;
            default:
                tablo = new MasterTable();
                break;
        }

        DataTable dt = tablo.GetAllRecord_Datatable(query.ToArray());
        Grid1.DataSource = dt;
        btnexcel.DataSource = dt;
    }

    public void OnNeedRebind(object sender, EventArgs oArgs)
    {
        System.Threading.Thread.Sleep(200);
        Grid1.DataBind();
    }

    public void OnNeedDataSource(object sender, EventArgs oArgs)
    {
        buildGrid();
    }

    public void OnPageChanged(object sender, ComponentArt.Web.UI.GridPageIndexChangedEventArgs oArgs)
    {
        Grid1.CurrentPageIndex = oArgs.NewIndex;
    }

    public void OnSort(object sender, ComponentArt.Web.UI.GridSortCommandEventArgs oArgs)
    {
        Grid1.Sort = oArgs.SortExpression;
    }

    private void InitializeComponent()
    {
        this.Load += new System.EventHandler(this.Page_Load);
        Grid1.NeedRebind += new ComponentArt.Web.UI.Grid.NeedRebindEventHandler(OnNeedRebind);
        Grid1.NeedDataSource += new ComponentArt.Web.UI.Grid.NeedDataSourceEventHandler(OnNeedDataSource);
        Grid1.PageIndexChanged += new ComponentArt.Web.UI.Grid.PageIndexChangedEventHandler(OnPageChanged);
        Grid1.SortCommand += new ComponentArt.Web.UI.Grid.SortCommandEventHandler(OnSort);
        Grid1.GroupCommand += new ComponentArt.Web.UI.Grid.GroupCommandEventHandler(OnGroup);
    }

    void Grid1_ItemCommand(object sender, ComponentArt.Web.UI.GridItemCommandEventArgs e)
    {

    }

    public void OnGroup(object sender, ComponentArt.Web.UI.GridGroupCommandEventArgs oArgs)
    {
        Grid1.GroupBy = oArgs.GroupExpression;
    }

    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    protected void btnGo_Click(object sender, EventArgs e)
    {
        MasterTable tablo;
        List<string> select = new List<string>();
        switch (SayfaTipi)
        {
            case PageType.Cari:
                select.Add("musteri_kod as [Müşteri Kodu]");
                select.Add("musteri_adi AS [Müşteri Adı]");
                select.Add("kart_sube AS [Kart Şubesi]");
                select.Add("ev_ilce AS [Ev Adresi İlçe]");
                select.Add("is_ilce AS [İş Adresi İlçe]");
                tablo = new Db_Musteri();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "musteri_kod like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "musteri_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Stok:
                select.Add("stok_kodu AS [Stok Kodu]");
                select.Add("stok_adi AS [Stok Adi]");
                tablo = new Db_Stok();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "stok_kodu like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "stok_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Reyon:
                break;
            case PageType.Depo:
                select.Add("depo_kodu AS [Depo Kodu]");
                select.Add("depo_adi AS [Depo Adı]");
                select.Add("sube_id AS [Şube No]");
                tablo = new Db_Depo();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "depo_kodu like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "depo_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Fiyat:
                select.Add("fyt_kod AS [Fiyat Kodu]");
                select.Add("fyt_adi AS [Fiyat Adı]");
                select.Add("fyt_kdv_dahil AS [Fiyat Kdv Dahil]");
                tablo = new Db_FiyatTanim();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "fyt_kod like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "fyt_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Pos:
                select.Add(" cash_id AS [Kasa No]");
                select.Add("cash_depo_id as [Depo No]");
                select.Add("cash_adi AS [Kasa Adı]");
                tablo = new Db_Poslar();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), " cash_id like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "cash_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Users:
                select.Add("usr_kod AS [Kullanici No]");
                select.Add("usr_adi AS [Kullanici Adi]");
                select.Add(" usr_tel AS [Kullanici Telefon]");
                select.Add(" usr_email AS E_mail");
                tablo = new Db_Users();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), " usr_kod like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "usr_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Firma:
                select.Add("frm_kod AS [Firma Kod]");
                select.Add(" frm_adi AS [Firma Adı]");
                select.Add("frm_adres AS [Firma Adres]");
                tablo = new Db_Firma();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), " frm_kod like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "frm_adi like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Donem:
                select.Add("dnm_kod AS [Dönem Kod]");
                select.Add(" dnm_firma_id AS [Firma No]");
                tablo = new Db_Donem();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), " dnm_kod like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "dnm_firma_id like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.Promosyon:
                select.Add("proh_kod AS [Promosyon Kodu]");
                select.Add(" proh_adi AS [Promosyon Adı]");
                tablo = new PromoHeader();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "proh_adi like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "proh_kod like '" + txtFiltre.Text + "%'");
                }
                break;
            case PageType.HizliPromosyon:
                select.Add("proh_kod AS [Promosyon Kodu]");
                select.Add(" proh_adi AS [Promosyon Adı]");
                tablo = new PromoHeader();
                if (drpFiltre.SelectedIndex == 1)
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "proh_adi like '" + txtFiltre.Text + "%'");
                }
                else
                {
                    Grid1.DataSource = tablo.GetAllRecord_Datatable(select.ToArray(), "proh_kod like '" + txtFiltre.Text + "%'");
                }
                break;
            default:
                break;
        }

        Grid1.DataBind();
    }
}
